package com.android.jay.lib_commin_ui.ratingbar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.android.jay.lib_commin_ui.R;

import androidx.annotation.Nullable;

/**
 * Created by wzp on 2020/2/20 0020.
 * <p>
 * 描述  ：自定义打分View
 */
public class CustomRatingBar extends View {


    private int mGradeNumber = 5; //总数
    private int mCurrentGrade = 0; //当前星数
    private Bitmap mStarFocusBitmap;
    private Bitmap mStartNormalBitmap;

    public CustomRatingBar(Context context) {
        this(context, null);
    }

    public CustomRatingBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomRatingBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomRatingBar);
        int startNormalId = ta.getResourceId(R.styleable.CustomRatingBar_startNormal, 0);
        if (startNormalId == 0) {
            throw new RuntimeException("请设置CustomRatingBar_startNormal ");
        }
        mStartNormalBitmap = BitmapFactory.decodeResource(getResources(), startNormalId);

        int starFocusId = ta.getResourceId(R.styleable.CustomRatingBar_startFocus, 0);
        if (startNormalId == 0) {
            throw new RuntimeException("请设置CustomRatingBar_startFocus ");
        }
        mStarFocusBitmap = BitmapFactory.decodeResource(getResources(), starFocusId);
        mGradeNumber = ta.getInt(R.styleable.CustomRatingBar_gradeNum, mGradeNumber);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int height = mStartNormalBitmap.getHeight();
        int width = (mStartNormalBitmap.getWidth() + getPaddingLeft()) * mGradeNumber;
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < mGradeNumber; i++) {
            int x = i * (mStartNormalBitmap.getWidth() + getPaddingLeft());
            if (mCurrentGrade > i) {
                canvas.drawBitmap(mStarFocusBitmap, x, 0, null);
            } else {
                canvas.drawBitmap(mStartNormalBitmap, x, 0, null);
            }

        }


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                float eventX = event.getX();
                Log.e("onTouchEvent ==", "onTouchEvent ==" + eventX +
                        "\n" + "mStartNormalBitmap=" + (mStartNormalBitmap.getWidth() + getPaddingLeft()));
                //获取当前评分
                int currentGrade = (int) (eventX / (mStartNormalBitmap.getWidth() + getPaddingLeft()));
                currentGrade += 1;
                if (currentGrade < 0) {
                    currentGrade = 0;
                }
//                if (currentGrade != mCurrentGrade) {
//                    mCurrentGrade =  currentGrade ;
//                }

                if (currentGrade == mCurrentGrade) {
                    return true;
                }
                // 再去刷新显示
                mCurrentGrade = currentGrade;
                invalidate();
                break;
        }
        return true;
    }


    public int getStarNumber() {
        return mCurrentGrade;
    }
}
